using System.Drawing;
using GTA;
using NativeUI;

namespace CustomPeds.Menus;

public class PropMenu : Script
{
	public static UIMenu menu = new UIMenu("", TextSetting.prop_Menu, new Point(0, -107));
	private static bool isMenuInitialized;

	public PropMenu()
	{
		if (!isMenuInitialized)
		{
			Main.pool.Add(menu);
			menu.OnMenuClose += OnMenuClose;
			menu.OnItemSelect += OnItemSelect;
			isMenuInitialized = true;
		}
	}

	public static void InitPropMenu()
	{
		menu.Clear();
		menu.SetBannerType(new UIResRectangle());
		menu.AddItem(new UIMenuItem(TextSetting.prop_MenuItem_Hats));
		menu.AddItem(new UIMenuItem(TextSetting.prop_MenuItem_Glasses));
		menu.AddItem(new UIMenuItem(TextSetting.prop_MenuItem_Earrings));
		menu.AddItem(new UIMenuItem(TextSetting.prop_MenuItem_WristAccessories1));
		menu.AddItem(new UIMenuItem(TextSetting.prop_MenuItem_WristAccessories2));
		for (int i = 0; i < menu.MenuItems.Count; i++)
		{
			menu.BindMenuToItem(PropSubMenu.menu, menu.MenuItems[i]);
		}
		menu.RefreshIndex();
		CheckCanUseMenuItem();
	}

	public static void CheckCanUseMenuItem()
	{
		Skin.Prop[] props;
		if (DressingScript.Dressing == Main.Member.P2)
		{
			Main.P2.skin.GetPropsValue();
			Main.P2.skin.GetPropsVariableNumber();
			props = Main.P2.skin.Props;
			_ = Main.P2.skin.PropsTextureData;
		}
		else
		{
			Main.P1.skin.GetPropsValue();
			Main.P1.skin.GetPropsVariableNumber();
			Main.Clone.skin.GetPropsValue();
			Main.Clone.skin.GetPropsVariableNumber();
			props = Main.P1.skin.Props;
			_ = Main.P1.skin.PropsTextureData;
		}
		for (int i = 0; i < menu.MenuItems.Count; i++)
		{
			menu.MenuItems[i].Enabled = false;
		}
		for (int j = 0; j <= 2; j++)
		{
			if (props[j].drawableNumber > 0)
			{
				menu.MenuItems[j].Enabled = true;
			}
		}
		if (props[6].drawableNumber > 0)
		{
			menu.MenuItems[3].Enabled = true;
		}
		if (props[7].drawableNumber > 0)
		{
			menu.MenuItems[4].Enabled = true;
		}
	}

	private void OnItemSelect(UIMenu sender, UIMenuItem item, int index)
	{
		if (index <= 2)
		{
			PropSubMenu.InitpropSubMenu(index);
		}
		if (index == 3)
		{
			PropSubMenu.InitpropSubMenu(6);
		}
		if (index == 4)
		{
			PropSubMenu.InitpropSubMenu(7);
		}
	}

	private void OnIndexChange(UIMenu sender, int index)
	{
	}

	private void OnMenuClose(UIMenu sender)
	{
		menu.Visible = false;
		menu.RefreshIndex();
	}
}
